<?php
namespace app\index\controller;

use think\View;
use think\Db;
use think\Request;

// 员工管理类
class Employee extends \think\Controller
{	
	// 加载首页页面
    public function loadIndex(){
		//echo substr('1234567890', -3, 3);exit;
		$view = new View();
        return $view->fetch('index');
	}
	
	// 加载首页页面数据
	public function loadIndexData(){	
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10; 
		
		$result = array();
	
		$result["total"] = Db::table('jdf_employee')->where('status',1)->count();		

		$bagin_record = ($page - 1) * $rows;
		$result["rows"] = Db::table('jdf_employee')->where('status',1)->order('id desc')->limit($bagin_record, $rows)->select();

		exit(json_encode($result));
	}
	
	
	// 根据员工号获得员工信息
	private function getEmployeeInfoByEmployeeNo($where = array() ){
		if(empty($where)) return array();		
		
		$r = Db::name('employee')->where($where)->find();
		
		return $r;		
	}
	

	// 加载添加员工页面
	public function loadAdd(){
		$request = Request::instance();
		$employee_id = $request->get('id');		
		return view('add',['id'=>$employee_id]);
	}
	
	// 加载添加员工认证信息页面
	public function loadAddTrian(){
		$request = Request::instance();
		$employee_no = $request->get('id');		
		return view('addTrain',['id'=>$employee_no]);		
	}
	
	// 判断新增或修改得员工号 是否可用
	public function checkEmployeeNo(){
		
		$request = Request::instance();
		
		$employee_no = $request->post('employee_no');
		$id = $request->post('id');
		
		$res = array();
		
		if(empty($employee_no)) {
			$res['code'] = false;
			$res['employee_no'] = $employee_no;
			$res['errorMsg'] = '员工号为空';
			
			exit(json_encode($res));
		}
		
		$where = array();
		$where = array('employee_no' => $employee_no);
		if(isset($id) && !empty($id)) {
			$where['id'] = array('neq', $id);
			$res['id'] = $id;
		} 	
		$row = $this->getEmployeeInfoByEmployeeNo($where);		
		
		
		if(!empty($row)) { // 修改信息 按照employee_no
			$res['code'] = false;
			$res['employee_no'] = $employee_no;
			$res['errorMsg'] = '员工号已存在';
		} else {
			$res['code'] = true;
			$res['employee_no'] = $employee_no;
			$res['errorMsg'] = '此员工号可用';
		}
		exit(json_encode($res));		
	}
	
	// 添加/修改信息
	public function saveEmployee(){
		
		$request = Request::instance();
				
		$_post = $request->post();
		
		if(empty($_post['id'])) {	
			unset($_post['id']);
			

			$_post['create_timestamp'] = date("Y-m-d H:i:s",time());
		
			Db::name('employee')->insert($_post);
		
			$userId = Db::name('employee')->getLastInsID();				
			
		} else if( $_post['id'] ) {			
			$employee_no = $_post['employee_no'];
			
			$where = array('employee_no' => $employee_no,'id' => array('neq', $_post['id']));
			
			$row = $this->getEmployeeInfoByEmployeeNo($where);
			
			if(!empty($row)) { // 修改信息 按照employee_no
				unset($_post['employee_no']);
			}
			
			$userId = Db::name('employee')->update($_post);			
		}
		
		if ($userId){
			exit(json_encode(array('success'=>true)));
		} else {
			exit(json_encode(array('errorMsg'=>'Some errors occured.')));
		}		
	}
	
	// 扫码获得员工信息
	public function employeeInfo(){
		
		$request = Request::instance();		
		$employee_no = $request->get('employee_no');		
		$userInfo = Db::name('employee')->where(array('employee_no' => $employee_no))->find();
		
		$view = new View();
		$view->employee_id = $userInfo['id'];
		$view->employee_no = $userInfo['employee_no'];
		$view->employee_name = $userInfo['employee_name'];
		//$view->employee_addtime = substr($userInfo['create_timestamp'],0,10);
		$view->join_date = $userInfo['join_date'];
		$view->departs = $userInfo['departs'];
		$view->departs_section = $userInfo['departs_section'];
		$view->section_room = $userInfo['section_room'];
		
		if(empty($userInfo['avatar'])){
			$view->avatar = '<img width="160px" height="160px" src="/static/images/default.jpg">';		
		} else {
			$view->avatar = '<img width="200px" height="200px" src="/static/images/default.jpg">';
		}
		
        return $view->fetch('info');	
	}
	
	// 根据员工号生成该用户的二维码图片
	public function createQRCode() {		
		$request = Request::instance();		
		$employee_no = $request->get('employee_no');
		
		$url=WEB_SITE_URL.'employeeInfo?employee_no='.$employee_no;
		require_once EXTEND_PATH.'/phpqrcode/phpqrcode.php';

		\QRcode::png($url,false,QR_ECLEVEL_L,14);
	}
	
	// 根据员工号查询该员工得认证经历
	public function getTrainInfo() {
		
		$request = Request::instance();
		
		$employee_id = $request->get('employee_id');

		
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10; 
		
		$result = array();
	
		$result["total"] = Db::name('training')->where(array('employee_id' => $employee_id))->count();		

		$bagin_record = ($page - 1) * $rows;
		$result["rows"] = Db::name('training')->where(array('employee_id' => $employee_id))->order('id desc')->limit($bagin_record, $rows)->select();

		exit(json_encode($result));
		
	}
	
	// 保存用户认证信息
	public function saveUserTrain(){
		
		$request = Request::instance();
		
		$_post = $request->post();
		
		$_post['create_timestamp'] = date("Y-m-d H:i:s",time());
		
		
		Db::name('training')->insert($_post);
		
		$userId = Db::name('training')->getLastInsID();
		
		if ($userId){
			exit(json_encode(array('success'=>true)));
		} else {
			exit(json_encode(array('errorMsg'=>'Some errors occured.')));
		}
	}
	
	// 获得用户信息
	public function getUserData(){
		
		$request = Request::instance();
		
		$employee_id = $request->get('employee_id');
		
		$row = $this->getEmployeeInfoByEmployeeNo(array('id' => $employee_id, 'status' => 1));;
		
		echo json_encode($row);
	}
}
